package com.example.luke.cashorderingsystem.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.example.luke.cashorderingsystem.Common.CommonActivity;
import com.example.luke.cashorderingsystem.R;
import com.example.luke.cashorderingsystem.Util.GlobalFunction;
import com.example.luke.cashorderingsystem.http.NetWorkConstant;


import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by luke on 25/05/2017.
 */

public class SettingActivity extends CommonActivity {

    @InjectView(R.id.ipAddress)EditText ipaddress;
    @InjectView(R.id.port)EditText port;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.setting_layout);
        initView();

    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.inject(this);
        ipaddress.setText(NetWorkConstant.defaultIp);
        port.setText(NetWorkConstant.defaultPort);
        showBackButton();

        setBarTitle("设置");
    }
    public void save(View view){

       if (checkRequirement()){
           NetWorkConstant.saveNetwork(this,ipaddress.getText().toString(),port.getText().toString());

           GlobalFunction.ToastAlert(this,"保存成功");
           finish();
       }


    }




    private boolean checkRequirement(){
        if (ipaddress.getText().toString().length()==0){
            GlobalFunction.ToastAlert(this,"请输入ip地址");
            return false;

        }else
        if (!NetWorkConstant.isIPAddress(ipaddress.getText().toString())){
            GlobalFunction.ToastAlert(this,"请输入有效ip");
            return false;

        }


        if (port.getText().toString().length()==0){
            GlobalFunction.ToastAlert(this,"请输入端口");
            return false;

        }


        return true;

    }

}
